package com.example.safelib

import android.app.Application
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.baselib.BaseViewModel
import com.example.baselib.constants.MyConstants
import com.example.baselib.databean.UserData
import com.example.safelib.bean.SafeNickBean
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

/**
 * Author: sxygsj
 * Date: 2020/8/25 9:25
 * Description:
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
class SafeAllViewModel(application: Application) : BaseViewModel(application)  {
    private val repository = SafeRepository()
    private val userData: UserData = MyConstants.userData
    private val allNickData: MutableLiveData<List<SafeNickBean>> by lazy { MutableLiveData<List<SafeNickBean>>().also { loadSafeNickData() } }

    fun getSafeNickData():LiveData<List<SafeNickBean>>{
        return  allNickData
    }
    fun loadSafeNickData()=launchUI {
        val result = repository.loadSafeNickData(userData.token)
        Log.i("+++++++++loadSafeData",result.body()?.data)
        val type = object : TypeToken<List<SafeNickBean>>() {}.type
        allNickData.value = Gson().fromJson<List<SafeNickBean>>(result.body()?.data, type)
    }
}